<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/21
 * Time: 11:14
 */

namespace app\admin\controller;
use app\admin\model\Base;
use think\Controller;
use think\Db;
use think\Request;

class Babyinfo extends Common
{
    public $obj="";
    public $table="babyinfo";
    public $date="";
public $go="babyinfo";
    public function __initialize(){
        parent::__initialize();

    }
    public function show(){
        //每页显示10条数据
        $list = Db::table('babyinfo')->order("id","desc")->paginate(10);
        //分页
        $page=$list->render();
        // 模板变量赋值
        $this->assign('list', $list);
        //分页输出显示
        $this->assign('page',$page);
        // 渲染模板输出
        return $this->fetch($this->go.'/show');
    }











//添加数据页面
    public function add(){
        $list = Db::table('userinfo')->select();
        $this->assign('list', $list);
        return $this->fetch();
    }
    //添加数据信息
    public function addm(){
        $object=new Base();
        $table=$this->table;
        $date=Request::instance()->post();
        $obj=$object->add($table,$date);
        if($obj){
            $this->success('添加成功', url('Babyinfo/show'));
        }else {
            $this->error('添加失败');
        }
    }
    public function addi(){
        $this->upload();
        $date=$_POST[''];

    }







//信息删除
    public function dels(){
        $obj=new Base();
        $id=input('id');
        //var_dump($id);
        $table=$this->table;
        $rs=$obj->del($table,$id);
        if($rs){
            $this->success('删除成功', url('Babyinfo/show'));
        }else {
            $this->error('删除失败');
        }


    }











//修改内容页面
public function update(){
    $id=input('id');
    $info=Db::table('babyinfo')->where("id=$id")->find();
    $this->assign('info', $info);
    $list = Db::table('ategory')->select();
    $this->assign('list', $list);
    return $this->fetch();
    }
    //修改内容
    public function updatem(){
        $object=new Base();
        $table=$this->table;
        $id=input('id');
        $date=Request::instance()->post();
        $date=array(
            "id"=>$date['id'],
            "baby_name"=>$date['baby_name'],
            "age"=>$date['age'],
            "sex"=>$date['sex'],
            "userinfo_id"=>$date['userinfo_id'],
            "birthday"=>$date['birthday'],
            "sort"=>$date['sort'],
        );
        $obj=$object->upd($table,$id,$date);
        if($obj){
            $this->success('修改成功', url('Babyinfo/show'));
        }else {
            $this->error('修改失败');
        }
    }










//多图片上传
    public function upload(){
        $dir_base = "./files/"; 	//文件上传根目录
        //没有成功上传文件，报错并退出。
        if(empty($_FILES)) {
            echo "<textarea><img src='{$dir_base}error.jpg'/></textarea>";
            exit(0);
        }
        $output = "<textarea>";
        $index = 0;		//$_FILES 以文件name为数组下标，不适用foreach($_FILES as $index=>$file)
        foreach($_FILES as $file){
            $upload_file_name = 'upload_file' . $index;		//对应index.html FomData中的文件命名
            $filename = $_FILES[$upload_file_name]['name'];
            $gb_filename = iconv('utf-8','gb2312',$filename);	//名字转换成gb2312处理
            //文件不存在才上传
            if(!file_exists($dir_base.$gb_filename)) {
                $isMoved = false;  //默认上传失败
                $MAXIMUM_FILESIZE = 1 * 1920 * 1080; 	//文件大小限制	1M = 1 * 1024 * 1024 B;
                $rEFileTypes = "/^\.(jpg|jpeg|gif|png){1}$/i";
                if ($_FILES[$upload_file_name]['size'] <= $MAXIMUM_FILESIZE &&
                    preg_match($rEFileTypes, strrchr($gb_filename, '.'))) {
                    $isMoved = @move_uploaded_file ( $_FILES[$upload_file_name]['tmp_name'], $dir_base.$gb_filename);		//上传文件
                }
            }else{
                $isMoved = true;	//已存在文件设置为上传成功
            }
            if($isMoved){
                //输出图片文件<img>标签
                //注：在一些系统src可能需要urlencode处理，发现图片无法显示，
                //    请尝试 urlencode($gb_filename) 或 urlencode($filename)，不行请查看HTML中显示的src并酌情解决。
                $output .= "<img src='{$dir_base}{$filename}' title='{$filename}' alt='{$filename}'/>";
            }else {
                $output .= "<img src='{$dir_base}error.jpg' title='{$filename}' alt='{$filename}'/>";
            }
            $index++;
        }

    }
}